package com.fingard.dsp.bank.directbank.lzcb01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class LZCB01Base extends DirectBase implements ITcpClient {

    protected void writeCommonHeader(XmlTextWriter xmlWriter, LZCB01Header lzcb01Header) throws Exception {
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("TrsCode", lzcb01Header.transCode);                                 //交易码
        xmlWriter.writeElementString("ERPTrsTimestamp",Format.DateTimeFormat.format(new Date()));                                                     //银企客户号
        xmlWriter.writeElementString("CifNo",bankFront.org);                                                         //用户号         //保留字段
        xmlWriter.writeEndElement();
    }

    protected String[] sendToBank(String p_reqStr,LZCB01Header header) throws Exception {
        String[] retStrArr = new String[]{"", ""};
        //http://xxxxx:[localPort]/xxx/*.do?userPassword=123456&SIGDATA=1
        String serverURL = bankFront.serverIP + ":" + bankFront.serverPort + "/xxx/" + header.transCode + ".do?userPassword=123456&SIGDATA=1";
        WriteBankLogLn("1.提交银行的地址：" + serverURL);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(serverURL);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Content-Type", "application/xml;charset=utf-8");
        tmpWebRequest.setRequestProperty("connection", "Close");
        byte[] tmpToWriteBytes = p_reqStr.getBytes(getCharset());
        retStrArr = tmpWebRequest.upload(tmpToWriteBytes, getCharset());

        return retStrArr;
    }

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    @Override
    public void getContentLength(TcpReceiver tcpReceiver) throws Exception {
        if (tcpReceiver.contentLength == -1) {
            if (tcpReceiver.readedLength >= 12) {
                String tmpHead = new String(tcpReceiver.recBytes, 0, 10, getCharset());
                tcpReceiver.contentLength = Integer.parseInt(tmpHead.trim()) - 2;
                tcpReceiver.contentStartIndex = 12;
            }
        }
    }

    //上送格式为json格式
    protected String[] sendToATS(String jsonData) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankLogLn("2.提交ATS的地址：" + FGBiz.dspCenter.dspSet.receiptPushServerUrl);


        //发送ATS
        WebRequest tmpRequest = new WebRequest(FGBiz.dspCenter.dspSet.receiptPushServerUrl);
        tmpRequest.setConnectTimeout(60000);
        tmpRequest.setRequestProperty("connection", "Close");
        tmpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        String charsetName = getCharset();
        WriteBankLogLn("3.推送给ATS的json数据" + jsonData);
        byte[] tmpWriteBytes = jsonData.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogLn("4.返回的数据" + retStr[1]);

        return retStr;
    }
}
